package com.allinpay.merchantweb.service.impl;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import com.allinpay.merchantweb.response.AppCommonResponse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

@Service
public class RestRequestService {
	
	@Autowired
	private CustomizedRestTemplate restTemplate;
	
	@Autowired
	Environment env;
	
	private String getAppHost() {
		return env.getProperty("app.APP_HOST");
	}
	/**
	 * 向tlm-app发送post请求(单笔请求)
	 * 
	 * @param url
	 * @param reqMap
	 * @return
	 */
	public AppCommonResponse postToAppForSingleByObj(String url, Map<String, Object> reqMap , Type typeOfT){
		Gson gson = new Gson();
		String reqBody = gson.toJson(reqMap);
		ResponseEntity<String> pson = restTemplate.postForEntity(getAppHost() + url, reqBody, String.class);
		String resBody = pson.getBody();
//		AppCommonResponse response = gson.fromJson(resBody, new TypeToken<AppCommonResponse>() {}.getType());
		AppCommonResponse response = gson.fromJson(resBody, typeOfT);
		if (response == null) {
			response = new AppCommonResponse();
		}
		return response;
	}

	public Map<String, Object> postToAppForMap(String url, Map<String, Object> reqMap) {
		Gson gson = new Gson();
		String reqBody = gson.toJson(reqMap);
		ResponseEntity<String> pson = restTemplate.postForEntity(getAppHost() + url, reqBody, String.class);
		Map<String, Object> resMap = gson.fromJson(pson.getBody(), new TypeToken<Map<String, Object>>() {}.getType());
		return resMap;
	}

	/**
	 * 向tlm-app发送post请求
	 * 
	 * @param url
	 * @param reqMap
	 * @return
	 */
	public Map<String, Object> postForEntityToApp(String url, Map<String, String> reqMap) {
		Gson gson = new Gson();
		String reqBody = gson.toJson(reqMap);
		ResponseEntity<String> pson = restTemplate.postForEntity(getAppHost() + url, reqBody, String.class);
		String resBody = pson.getBody();
		Map<String, Object> resMap = null;
		if (StringUtils.isBlank(resBody)) {
			return resMap;
		} else {
			resMap = gson.fromJson(resBody, new TypeToken<Map<String, Object>>() {}.getType());
			return resMap;
		}
	}

	public List<Object> postToAppMaps(String url, Map<String, Object> reqMap) {
		Gson gson = new Gson();
		String reqBody = gson.toJson(reqMap);
		ResponseEntity<String> pson = restTemplate.postForEntity(getAppHost() + url, reqBody, String.class);
		List<Object> list = gson.fromJson(pson.getBody(), new TypeToken<List<Object>>() {}.getType());
		return list;
	}

//	/**
//	 * 向tlm-app发送post请求(分页查询)
//	 * 
//	 * @param url
//	 * @param reqMap
//	 * @return
//	 */
//	public Page postToAppForPageSelect(String url, Map<String, String> reqMap) {
//		Gson gson = new Gson();
//		String reqBody = gson.toJson(reqMap);
//
//		ResponseEntity<String> pson = restTemplate.postForEntity(apphost + url, reqBody, String.class);
//		String resBody = pson.getBody();
//
//		Page page = gson.fromJson(resBody, new TypeToken<Page>() {}.getType());
//		return page;
//	}

	/**
	 * 向tlm-app发送post请求(单笔请求)
	 * 
	 * @param url
	 * @param reqMap
	 * @return
	 */
	public AppCommonResponse postToAppForSingle(String url, Map<String, String> reqMap, Type typeOfT) {
		Gson gson = new Gson();
		String reqBody = gson.toJson(reqMap);
		ResponseEntity<String> pson = restTemplate.postForEntity(getAppHost() + url, reqBody, String.class);
		String resBody = pson.getBody();
//		AppCommonResponse response = gson.fromJson(resBody, new TypeToken<AppCommonResponse>() {}.getType());
		AppCommonResponse response = gson.fromJson(resBody, typeOfT);
		if (response == null) {
			response = new AppCommonResponse();
		}
		return response;
	}
	
	
	public AppCommonResponse postToAppForSingle(String url, Map<String, String> reqMap) {
		Gson gson = new Gson();
		String reqBody = gson.toJson(reqMap);
		ResponseEntity<String> pson = restTemplate.postForEntity(getAppHost() + url, reqBody, String.class);
		String resBody = pson.getBody();
		AppCommonResponse response = gson.fromJson(resBody, new TypeToken<AppCommonResponse>() {}.getType());
		if (response == null) {
			response = new AppCommonResponse();
		}
		return response;
	}

	/**
	 * 向tlm-app发送post请求
	 * 
	 * @param url
	 * @param reqMap
	 * @return
	 */
	public List<Object> postForListEntityToApp(String url, Object obj) {
		Gson gson = new Gson();
		String reqBody = gson.toJson(obj);
		ResponseEntity<String> pson = restTemplate.postForEntity(getAppHost() + url, reqBody, String.class);
		String resBody = pson.getBody();
		List<Object> list = null;
		if (StringUtils.isBlank(resBody)) {
			return list;
		} else {
			list = gson.fromJson(resBody, new TypeToken<List<Object>>() {}.getType());
			return list;
		}
	}

	/**
	 * 向tlm-app发送post请求(分页查询)
	 * 
	 * @param url
	 * @param reqMap
	 * @return
	 */
//	public Page postToAppForPageSelectByObj(String url, Object obj) {
//		Gson gson = new Gson();
//		String reqBody = gson.toJson(obj);
//		ResponseEntity<String> pson = restTemplate.postForEntity(apphost + url, reqBody, String.class);
//		String resBody = pson.getBody();
//		Page page = gson.fromJson(resBody, new TypeToken<Page>() {}.getType());
//		return page;
//	}

	/**
	 * 向tlm-app发送post请求(单笔请求)(精确的制定返回的bean类型)
	 * 
	 * @param url
	 * @param obj
	 * @return
	 */
	public AppCommonResponse postToAppForSingleByObj(String url, Object obj, Type typeOfT) {
		Gson gson = new Gson();
		String reqBody = gson.toJson(obj);
		ResponseEntity<String> pson = restTemplate.postForEntity(getAppHost() + url, reqBody, String.class);
		String resBody = pson.getBody();
		System.out.println("postToAppForSingleByObj recv:" + resBody);
		AppCommonResponse response = gson.fromJson(resBody, typeOfT);
		if (response == null) {
			response = new AppCommonResponse();
		}
		return response;
	}
	
	
	/**
	 * 向tlm-app发送post请求(单笔请求)
	 * 
	 * @param url
	 * @param obj
	 * @return
	 */
	public AppCommonResponse postToAppForSingleByObj(String url, Object obj) {
		Gson gson = new Gson();
		String reqBody = gson.toJson(obj);
		ResponseEntity<String> pson = restTemplate.postForEntity(getAppHost() + url, reqBody, String.class);
		String resBody = pson.getBody();
		AppCommonResponse response = gson.fromJson(resBody, new TypeToken<AppCommonResponse>() {}.getType());
		if (response == null) {
			response = new AppCommonResponse();
		}
		return response;
	}

	public String postToAppByJson(String url, String json) {
		ResponseEntity<String> pson = restTemplate.postForEntity(getAppHost() + url, json, String.class);
		return pson.getBody();
	}




}
